;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.PlanningAccuracy", [])
        .controller("PlanningAccuracyCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.markInfo = {
                allChecked: false
            };
            // 分页信息
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            // 标签页信息
            $scope.activeFlag = [true, false];
            // 按钮权限
            $scope.btnPermission = {
                export: true
            };
            // 高级搜索配置
            $scope.toolbarConfig = {
                showColConfig: false
            };
            $scope.tbConf = {
                scrollWidth: 1800,
                tableCol: [
                    {
                        type: "deliveryDate",
                        name: "交付日期",
                        dataField: "deliveryDate",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };
            $scope.viewTbConf = {
                scrollWidth: 1130,
            };
            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/report/exportPlanningAccuracy"),
            };
            // 搜索条件
            $scope.condition = {};
            // 所有月份信息
            $scope.monthData = [
                {name: '1月', key: 'jan', value: 1, isChecked: true, isSearch: true}, {name: '2月', key: 'feb', value: 2, isChecked: true, isSearch: true},
                {name: '3月', key: 'mar', value: 3, isChecked: true, isSearch: true}, {name: '4月', key: 'apr', value: 4, isChecked: true, isSearch: true},
                {name: '5月', key: 'may', value: 5, isChecked: true, isSearch: true}, {name: '6月', key: 'jun', value: 6, isChecked: true, isSearch: true},
                {name: '7月', key: 'jul', value: 7, isChecked: true, isSearch: true}, {name: '8月', key: 'aug', value: 8, isChecked: true, isSearch: true},
                {name: '9月', key: 'sept', value: 9, isChecked: true, isSearch: true}, {name: '10月', key: 'oct',value: 10, isChecked: true, isSearch: true},
                {name: '11月', key: 'nov', value: 11, isChecked: true, isSearch: true}, {name: '12月', key: 'dec', value: 12, isChecked: true, isSearch: true}
            ];

            $scope.tbConf = [
                {name: '月度订单', month: 'jan', field: 'janMonthCount', isShow: true}, {name: '紧急订单', month: 'jan', field: 'janUrgencyCount', isShow: true}, {name: '准确率', month: 'jan', field: 'janRate', isShow: true},
                {name: '月度订单', month: 'feb', field: 'febMonthCount', isShow: true}, {name: '紧急订单', month: 'feb', field: 'febUrgencyCount', isShow: true}, {name: '准确率', month: 'feb', field: 'febRate', isShow: true},
                {name: '月度订单', month: 'mar', field: 'marMonthCount', isShow: true}, {name: '紧急订单', month: 'mar', field: 'marUrgencyCount', isShow: true}, {name: '准确率', month: 'mar', field: 'marRate', isShow: true},
                {name: '月度订单', month: 'apr', field: 'aprMonthCount', isShow: true}, {name: '紧急订单', month: 'apr', field: 'aprUrgencyCount', isShow: true}, {name: '准确率', month: 'apr', field: 'aprRate', isShow: true},
                {name: '月度订单', month: 'may', field: 'mayMonthCount', isShow: true}, {name: '紧急订单', month: 'may', field: 'mayUrgencyCount', isShow: true}, {name: '准确率', month: 'may', field: 'mayRate', isShow: true},
                {name: '月度订单', month: 'jun', field: 'junMonthCount', isShow: true}, {name: '紧急订单', month: 'jun', field: 'junUrgencyCount', isShow: true}, {name: '准确率', month: 'jun', field: 'junRate', isShow: true},
                {name: '月度订单', month: 'jul', field: 'julMonthCount', isShow: true}, {name: '紧急订单', month: 'jul', field: 'julUrgencyCount', isShow: true}, {name: '准确率', month: 'jul', field: 'julRate', isShow: true},
                {name: '月度订单', month: 'aug', field: 'augMonthCount', isShow: true}, {name: '紧急订单', month: 'aug', field: 'augUrgencyCount', isShow: true}, {name: '准确率', month: 'aug', field: 'augRate', isShow: true},
                {name: '月度订单', month: 'sept', field: 'septMonthCount', isShow: true}, {name: '紧急订单', month: 'sept', field: 'septUrgencyCount', isShow: true}, {name: '准确率', month: 'sept', field: 'septRate', isShow: true},
                {name: '月度订单', month: 'oct', field: 'octMonthCount', isShow: true}, {name: '紧急订单', month: 'oct', field: 'octUrgencyCount', isShow: true}, {name: '准确率', month: 'oct', field: 'octRate', isShow: true},
                {name: '月度订单', month: 'nov', field: 'novMonthCount', isShow: true}, {name: '紧急订单', month: 'nov', field: 'novUrgencyCount', isShow: true}, {name: '准确率', month: 'nov', field: 'novRate', isShow: true},
                {name: '月度订单', month: 'dec', field: 'decMonthCount', isShow: true}, {name: '紧急订单', month: 'dec', field: 'decUrgencyCount', isShow: true}, {name: '准确率', month: 'dec', field: 'decRate', isShow: true}
            ];

            $scope.dataList = [];
            $scope.doNotShowMonth = [];

            // 选择月份筛选条件
            $scope.selectMonth = function () {
                var checkedMonth = [];          // 已选中的月份
                var monthCount = [];            // 调接口筛选的参数
                angular.forEach($scope.monthData, function (item) {
                    if (item.isChecked) {
                        checkedMonth.push(item.name);
                        monthCount.push(item.value);
                    }
                });
                $scope.condition.monthCount = monthCount;
                $scope.condition.months = checkedMonth.join(',');
            };

            // 高级搜索确认搜索
            $scope.confirmSearch = function () {
                var doNotShowMonth = [];        // 不要展示的月份  表头去掉
                angular.forEach($scope.monthData, function (item) {
                    if (!item.isChecked) {
                        doNotShowMonth.push(item.key);
                        item.isSearch = false;
                    } else {
                        item.isSearch = true;
                    }
                });
                angular.forEach($scope.tbConf, function (item) {
                    if (doNotShowMonth.indexOf(item.month) >= 0) {
                        item.isShow = false;
                    } else {
                        item.isShow = true;
                    }
                });
                $scope.doNotShowMonth = doNotShowMonth;
                $scope.searchData(1);
            };

            // 切换高级搜索
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            //重置搜索条件
            $scope.reset = function () {
                $scope.condition = {};
                $scope.doNotShowMonth = [];
                angular.forEach($scope.monthData, function (item) {
                    item.isChecked = true;
                    item.isSearch = true;
                });
                angular.forEach($scope.tbConf, function (item) {
                    item.isShow = true;
                });
                $scope.searchData(1);
            };

            // 按钮权限code
            var btnPermissionCode = {
                export: window.ActionCode.planningAccuracyExport
            };

            // 设置按钮权限
            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }
            // 时间选择器配置
            $scope.dateOptions = {
                minMode:'year',
                datepickerMode: 'year'
            };

            // 获取报表列表
            $scope.searchData = function (curPage) {
                if (!$scope.condition.date) {
                    ybWidget.ShowMessage('年份不能为空，请选择年份！', MESG_TYPE.Error);
                    return;
                }
                var url = '/report/planningAccuracy';
                if (curPage) $scope.pageInfo.curPage = curPage;
                var params = {
                    filters: {
                        monthCount: $scope.condition.monthCount,
                        purchaseOrgName: $scope.condition.purchaseOrgName,
                        date:$scope.condition.date
                    },
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage
                };
                ybHttp.Post('device', url, params).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    $scope.pageInfo.totalRows = res.value.total;
                    $scope.pageInfo.totalPages = res.value.pages;
                    $scope.dataList = res.value.list;
                })
            };

            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
            })()
        }])
    ;
})(window, window.angular);